package com.gilight.base.admin;

import com.gilight.base.component.CacheComponent;
import com.gilight.base.mapper.ISPMapper;
import com.gilight.base.model.ISPDo;
import com.gilight.base.model.SysZzjg;
import com.github.binarywang.wxpay.bean.request.WxPayDownloadBillRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;

@Service
public class UtilServices {
    @Autowired
    private ISPMapper ispMapper;
    @Autowired
    private CacheComponent cacheComponent;
    private volatile ConcurrentHashMap<Long,WxPayService> cachWxPayServices = new ConcurrentHashMap<>();
    public WxPayService getOtherISPService(SysZzjg zzjg){
        WxPayService otherWxpService = null;
        if(zzjg == null){
            return otherWxpService;
        }
        Long zzjgIspId = zzjg.getZzjgIspid();
        otherWxpService =  this.getOtherISPService(zzjgIspId);
        return otherWxpService;
    }
    public WxPayService getOtherISPService(Long zzjgIspId){
        WxPayService otherWxpService = null;
        if(zzjgIspId==null){
            return null;
        }
        if(cachWxPayServices.containsKey(zzjgIspId)){
            return cachWxPayServices.get(zzjgIspId);
        }
        synchronized (this) {
            /**
             * 双重判断
             */
            if(cachWxPayServices.containsKey(zzjgIspId)){
                return cachWxPayServices.get(zzjgIspId);
            }
            ISPDo ispDo = ispMapper.selectById(zzjgIspId);
            String muchId = ispDo.getIspMucId();
            String muchKey = ispDo.getIspMucKey();
            String keyPath = ispDo.getIspCert();
            String notyUrl = ispDo.getNotyUrl();
            if (zzjgIspId != null) {//如果当前商户被指定了其它的服务商那么加载该服务商的配置文件
                otherWxpService = new WxPayServiceImpl() {
                    @Override
                    public WxPayConfig getConfig() {
                        WxPayConfig wxPayConfig = new WxPayConfig();
                        wxPayConfig.setMchId(muchId);
                        wxPayConfig.setMchKey(muchKey);
                        wxPayConfig.setNotifyUrl(notyUrl);
                        wxPayConfig.setKeyPath(keyPath);
                        return wxPayConfig;
                    }
                };
                cachWxPayServices.put(zzjgIspId,otherWxpService);
            }
        }
        return otherWxpService;
    }

    public void doSpecISP(SysZzjg sysZzjg, WxPayUnifiedOrderRequest orderRequest) {
        Long zzjgIspId = sysZzjg.getZzjgIspid();
        if(zzjgIspId == null) return;
        ISPDo ispDo = ispMapper.selectById(zzjgIspId);
        Integer sameBody = ispDo.getSameBody();
        if(sameBody == 0){ //如果不是相同主体
            String appId = ispDo.getMiniAppId(); //服务商所关联的小程序列ID
            //那么此时orderRequest        orderRequest.getAppid() 将变成sup_appid
            String subAppId = orderRequest.getAppid();
            orderRequest.setSubAppId(subAppId);
            orderRequest.setAppid(appId);
            //此时openid应该换成sub_openid
            String openId = orderRequest.getOpenid();
            orderRequest.setOpenid("");
            orderRequest.setSubOpenid(openId);
        }else{
            orderRequest.setSubAppId("");
        }

    }
    public void doSpecISPDownBill(SysZzjg sysZzjg, WxPayDownloadBillRequest wxPayDownloadBillRequest) {
        Long zzjgIspId = sysZzjg.getZzjgIspid();
        if(zzjgIspId == null) return;
        ISPDo ispDo = ispMapper.selectById(zzjgIspId);
        Integer sameBody = ispDo.getSameBody();
            String muchId = ispDo.getIspMucId();
            wxPayDownloadBillRequest.setMchId(muchId);
            //那么此时orderRequest        orderRequest.getAppid() 将变成sup_appid
            //此时openid应该换成sub_openid


    }
}
